package com.ctz.commonframe.utils;

import java.util.concurrent.LinkedBlockingQueue;

public abstract class AbsBlockedQueueThread<T> extends Thread{
	public abstract void handleData(T data);
	
	private LinkedBlockingQueue<T> queue;
	private boolean runFlag;
	
	public AbsBlockedQueueThread(){
		super();
		queue = new LinkedBlockingQueue<T>();
	}
	
	public void addData(T data){
		try {
			queue.put(data);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public void startRun(){
		if(!runFlag){
			runFlag = true;
			start();
		}
	}
	
	public void stopRun(){
		runFlag = false;
		interrupt();
	}
	
	public boolean isRunning(){
		return runFlag;
	}
	
	@Override
	public void run() {
		while(runFlag){
			try {
				T data = queue.take();
				handleData(data);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
